package com.example.config;

import com.example.servlet.MyListener;
import com.example.servlet.WebFilter;
import com.example.servlet.WebServlet;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Collections;

@Configuration
public class ServletConfig {

    @Bean
    public ServletRegistrationBean<WebServlet> servletRegistrationBean() {
        ServletRegistrationBean<WebServlet> servlet = new ServletRegistrationBean<WebServlet>();
        servlet.setServlet(new WebServlet());
        servlet.setUrlMappings(Collections.singletonList("/web"));
        return servlet;
    }

    @Bean
    public FilterRegistrationBean<WebFilter> filterRegistrationBean() {
        FilterRegistrationBean<WebFilter> filter = new FilterRegistrationBean<>();
        filter.setFilter(new WebFilter());
        filter.setUrlPatterns(Collections.singletonList("/"));
        return filter;
    }

    @Bean
    public ServletListenerRegistrationBean<MyListener> listenerRegistrationBean() {
        ServletListenerRegistrationBean<MyListener> listener = new ServletListenerRegistrationBean<>();
        listener.setListener(new MyListener());
        return listener;
    }


}



